package com.siwei.mes.config;

import cn.dev33.satoken.jwt.StpLogicJwtForStateless;
import cn.dev33.satoken.stp.StpLogic;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.util.MacTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

/**
 * @author chenmaolin
 * @date 2021-11-19
 */
@Slf4j
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    @Value(value = "${file.mac}")
    private String FILE_MAC;
    @Value(value = "${file.windows}")
    private String FILE_WINDOWS;
    @Value(value = "${file.linux}")
    private String FILE_LINUX;

    @Value(value = "${excel.mac}")
    private String EXCEL_MAC;
    @Value(value = "${excel.windows}")
    private String EXCEL_WINDOWS;
    @Value(value = "${excel.linux}")
    private String EXCEL_LINUX;

    /**
     * 根据服务类型获取磁盘路径
     * 默认是windows磁盘路径
     *
     * @return
     */
    public String getFilePathName() {
        String address = null;
        String operSystem = MacTools.getOperSystem();
        if (Arrays.stream(BaseParams.MAC_LIST).parallel().anyMatch(operSystem::contains)) {
            address = FILE_MAC;
        } else if (Arrays.stream(BaseParams.LINUX_LIST).parallel().anyMatch(operSystem::contains)) {
            address = FILE_LINUX;
        } else {
            address = FILE_WINDOWS;
        }
        log.info("项目启动成功:操作系统：{},上传图片盘符地址：{}", operSystem, address);
        return address;
    }


    public String getExcelPathName() {
        String address = null;
        String operSystem = MacTools.getOperSystem();
        if (Arrays.stream(BaseParams.MAC_LIST).parallel().anyMatch(operSystem::contains)) {
            address = EXCEL_MAC;
        } else if (Arrays.stream(BaseParams.LINUX_LIST).parallel().anyMatch(operSystem::contains)) {
            address = EXCEL_LINUX;
        } else {
            address = EXCEL_WINDOWS;
        }
        log.info("项目启动成功:操作系统：{},上传EXCEL盘符地址：{}", operSystem, address);
        return address;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("初始化 sa-token 配置");
        // 注册登录拦截器（自定义）， 排除不需要注解鉴权的接口地址
        registry.addInterceptor(new SaTokenLoginInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/websocket/**").excludePathPatterns("/logs", "/static/**")
                .excludePathPatterns("/image/**").excludePathPatterns("/excel/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        /**
         * 资源映射路径
         * addResourceHandler：访问映射路径
         * addResourceLocations：资源绝对路径
         */
        registry.addResourceHandler("/image/**").addResourceLocations("file:" + getFilePathName());
        registry.addResourceHandler("/excel/**").addResourceLocations("file:" + getExcelPathName());
    }

    @Bean
    public StpLogic getStpLogicJwt() {
        return new StpLogicJwtForStateless();
    }
}
